import configparser
import os

CONFIG_FILE = 'config.ini'
EXAMPLE_CONFIG_FILE = 'example_config.ini'

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

ABS_CONFIG_FILE = os.path.join(BASE_DIR, CONFIG_FILE)


class LOCAL_SF(object):
    DB_HOST = ''
    DB_PORT = 0
    DB_USER = ''
    DB_PASSWORD = ''
    # 测试库
    DB_DEFAULT_DB = ''
    # 服务器连接 等待超时时间 (秒)
    DB_WAIT_TIMEOUT = 0


def check_config():
    if not os.path.exists(ABS_CONFIG_FILE):
        raise Exception(f'参考`{EXAMPLE_CONFIG_FILE}`文件，进行配置。文件名为: {CONFIG_FILE}')


def read_config():
    check_config()
    #
    config = configparser.ConfigParser()
    config.read(ABS_CONFIG_FILE)
    db_conf = config['DATABASE']
    LOCAL_SF.DB_HOST, LOCAL_SF.DB_PORT = db_conf['host'], int(db_conf['port'])
    LOCAL_SF.DB_USER, LOCAL_SF.DB_PASSWORD = db_conf['user'], db_conf['password']
    LOCAL_SF.DB_DEFAULT_DB, LOCAL_SF.DB_WAIT_TIMEOUT = db_conf['db'], int(db_conf['wait_timeout'])


read_config()
